var express = require('express');
var mysql = require('mysql');
var bodyParser = require('body-parser');

var app = express();

app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
//运行跨域
app.use(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    next();
});
// 1. 创建连接
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'ywj123',
    database: 'teacher',
    useConnectionPooling: true,
});
connection.connect();
app.get('/getTeacherData',function (req,res) {
    var sql = `select * from user`;
    connection.query(sql,function (err,data) {
        if (err){
            console.log(err.message);
            return;
        }else{
            if(data.length === 0){
                res.json({status:1,message:'rejected'});
            }else{
                res.json({status:0,message:data});
            }
            
        }
    })
});
app.post('/addTeacherData',(req,res)=>{
    //console.log(req.body);
    let name = req.body.name;
    let w_name = req.body.w_name;
    let age = req.body.age;
    let sex = req.body.sex;
    let phone = req.body.phone;
    let address = req.body.address;
    let isuse = req.body.isuse;
    let sql = `insert into user (name,w_name,age,sex,phone,address,isuse) values ('${name}','${w_name}',${age},'${sex}','${phone}','${address}','${isuse}')`;
    connection.query(sql,function (err,data) {
        if (err){
            console.log(err.message);
            return;
        }else{
            res.json({status:0,message:'添加成功'}); 
        }
    })
});
app.post('/changeIsuse',(req,res)=>{
    let key = req.body.key;
    let isuse = req.body.data;
    let sql = `UPDATE user SET isuse = '${isuse}' WHERE id = '${key}'`;
    //console.log(sql);
    connection.query(sql,function(err,data){
        if(err){
            console.log(err.message);
            return;
        }else{
            res.json({status:0,message:'修改成功'});
        }
    })
});
app.get('/deleteItem',(req,res)=>{
    let id = req.query.id;
    console.log(id);
    let sql = `delete from user where id=${id}`;
    connection.query(sql,(err,data)=>{
        if(err){
            console.log(err.message);
            return;
        }else{
            res.json({status:0,message:'删除成功'});
        }
    })
});
app.post('/checkLogin',function (req,res) {
	//console.log(qs.parse(req.body));
    var username = req.body.username;
    var password = req.body.password;
	//console.log(username,password);
    var sql = `SELECT * FROM user WHERE name='${username}' AND password='${password}'`;
    //console.log(sql);
    connection.query(sql,function (err,result) {
        if (err){
            console.log(err.message);
            return;
        }else{
			console.log(result);
			if(result.length === 0){
				console.log("1");
				res.json({status:1,message:'登录失败'});
			}else{
				res.json({status:0,message:'登录成功'});
			}
            //console.log(result)
        }
    })
});
app.listen(3003,()=>{
    console.log('server is running...');
})